package com.testing.utils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class LoginUtils {

    private static final String HMAC_SHA1 = "HmacSHA1";
    private static final String CHARSET_NAME_UTF8 = "UTF-8";
    private static final char[] digital = "0123456789ABCDEF".toCharArray();

    private static LoginUtils loginUtils;

    public static LoginUtils getInstance() {
        if (loginUtils == null) {
            loginUtils = new LoginUtils();
        }
        return loginUtils;
    }

    public  byte[] hmacSha1(String[] datas, byte[] key) {
        SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_SHA1);
        Mac mac = null;
        try {
            mac = Mac.getInstance(HMAC_SHA1);
            mac.init(signingKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        try {
            for (String data : datas) {
                mac.update(data.getBytes(CHARSET_NAME_UTF8));
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return mac.doFinal();
    }

    public  String encodeHexStr(final byte[] bytes){
        if(bytes == null){
            return null;
        }
        char[] result = new char[bytes.length * 2];
        for (int i = 0; i < bytes.length; i++) {
            result[i*2] = digital[(bytes[i] & 0xf0) >> 4];
            result[i*2 + 1] = digital[bytes[i] & 0x0f];
        }
        return new String(result);
    }

    public  byte[] toBytes(final String str) {
        if (str == null) {
            return null;
        }
        try {
            return str.getBytes(CHARSET_NAME_UTF8);
        } catch (final UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public String getSignatureStr(Map<String,String> map){
        List<String> paramValueList = new ArrayList<String>();
        if(map != null){
            for (Map.Entry<String, String> entry : map.entrySet()) {
                paramValueList.add(entry.getKey() + entry.getValue());
            }
        }
        final String[] datas = new String[paramValueList.size()];
        Collections.sort(paramValueList);
        for (int i = 0; i < paramValueList.size(); i++) {
            datas[i] = paramValueList.get(i);
        }
        byte[] signature = hmacSha1(datas, toBytes("~leyoujia&applogin#$%"));
        String signatureStr=encodeHexStr(signature);
//        map.put("signature",signatureStr);
        return signatureStr;
    }

    public static void main(String[] args) {
        LoginUtils loginUtils = new LoginUtils();
        Map<String,String> map = new HashMap<>();
        map.put("mobile","1111");
        String p = loginUtils.getSignatureStr(map);
        System.out.println(p);
    }
}
